<template>
	<class-list-warp>
		<template #list-item>
			<view class="class-item text-16" v-for="(item,index) in classStore.classListFilter" :key="index" @click.stop="goToClass(item)" style="position: relative;">
				<view class="class-title">
					{{item.Title}}
					<view>
						<text :class="'tagbase tglstype'+item.LessonType">{{item.LessonTypeText}}</text>
					</view>
					<view>
						<text :class="'tagbase tgstatus'+item.Status">{{item.StatusTitle}}</text> 
					</view>
				</view>
				<view class="class-time">授课时间：{{item.StudyDay}} {{item.StartTime}} - {{item.EndTime}}</view>
				<view class="class-teacher">学生姓名：{{item.StudentName}}</view> 
				<view class="class-room" v-if="item.Status == 2 || item.Status == 4"> 
					<view style="flex:1;">
						<view v-if="item.RoomLink==null" style="flex:1;">课程链接：暂无</view>
						<!-- <u-link v-else :href="item.RoomLink" under-line>课程链接</u-link> -->
						<u-tag @click="enterroom(item)" text="进入课程" mode="light" />
					</view>
					<view style="flex:1;">密码：{{item.RoomPwd==null?'暂无':item.RoomPwd}}</view>
				</view> 
				<view class="class-room" v-if="item.Status==1"> 
					<view style="flex:1;"><u-tag text="确认课程" mode="light" @click="froom(item)" /></view>
				</view>
				<view class="class-evaluate" v-if="item.Status===5">
					<template v-if="[null, undefined].includes(item.ToTeacherEvaluateScore)">
						对教师的评价：暂无
					</template>
					<view class="flex" v-else>
						<view class="">对教师的评价：</view>
						<view class="">
							<u-rate :count="5" disabled v-model="item.ToTeacherEvaluateScore"></u-rate>
							<view v-if="item.ToTeacherEvaluateContent">{{item.ToTeacherEvaluateContent}}</view>
						</view>
					</view>
				</view>
				<view class="class-evaluate" v-if="item.Status===5">
					<view class="flex" v-if="![null, undefined].includes(item.ToStudentEvaluateScore)">
						<view class="">对学员的评价：</view>
						<view class="">
							<u-rate :count="5" disabled v-model="item.ToStudentEvaluateScore"></u-rate>
							<view v-if="item.ToStudentEvaluateContent">{{item.ToStudentEvaluateContent}}</view>
						</view>
					</view>
					<template v-else>对学员的评价：</template>
					<u-tag v-if="[null, undefined].includes(item.ToStudentEvaluateScore)" @click="handleEvaluate(item)" text="评价" mode="light" />
				</view>
				<view class="class-links">
					<u-link v-if="item.Video==null && (item.Status == 5 || item.Status == 6)" @click="uploadvideo(item)" class="link-item" under-line>上传课程</u-link>
					<u-link v-if="item.Video" :href="item.Video" class="link-item" under-line>课程回看</u-link>
					<u-link v-if="item.Status>=5" @click="classStore.handleToClassReport(item)" class="link-item" under-line>课堂报告</u-link>
				</view>
			</view> 
		</template>
	</class-list-warp>
	<u-popup v-model="visibleEvaluate" mode="top" height="600rpx" z-index="998">
		<view style="margin-top: 145rpx;padding: 50rpx; display: flex; flex-flow: column; align-items: flex-start;">
			<view  class="mb-5rpx">评价分数</view>
			<u-rate :count="5" v-model="StudentEvaluateScore"></u-rate>
			<u-input v-model="EvaluateContent" type="textarea" class="w-full mt-16rpx" :border="true" placeholder="请输入评价内容" :height="100" :auto-height="true" />
			<u-button class="mt-30rpx w-full" type="primary" @click="handleSetEvaluate">提交</u-button>
		</view>
	</u-popup>
</template>

<script lang="ts" setup>
	import { ref } from 'vue';
	import classListWarp from './class-list-warp.vue';
	import { useClassStore } from '@/store/class';
	import { TXRoom, ktlapi } from '@/utils';
	import { HTTP_REQUEST_INFO } from '@/common/config';
	
	/** 课程列表：教师 */
	defineOptions({ name: 'class-list-teacher', inheritAttrs: false });
	
	const classStore = useClassStore();
	
	const visibleEvaluate = ref(false);
	const hintData = ref(null);
	const StudentEvaluateScore = ref(0);
	const EvaluateContent = ref('');
	
	
	function enterroom(item: any){
		console.log(item);
		TXRoom(item.RoomLink,item.RoomNo,item.RoomPwd);
	}
	
	function goToClass(data: any){
		if (![2,4].includes(data.Status)) {
			uni.showToast({
				title: '课程未开始或已结束',
				icon:'none'
			})
			return;
		}
		 
		if(data.LessonType == 1){
			uni.navigateTo({
				url: '/pages-study/study/study?LessonId=' + data.UUID
			})
		}else{
			uni.setStorageSync("ReviewUUID",data.UUID);
			uni.setStorageSync("ReviewAccountID",data.StudentAccountID);
			uni.navigateTo({
				url: '/pages-review/review/review?ReviewUUID='+data.UUID+'&ReviewAccountID='+data.StudentAccountID
			})    
		}
	}
	
	function handleEvaluate(item: any){
		hintData.value = item;
		visibleEvaluate.value = true;
	}
	
	function handleSetEvaluate(){
		ktlapi({
			action: 'Cus_SetLessonEvaluate',
			data: {
				LessonUUID: hintData.value.UUID,
				Score: StudentEvaluateScore.value,
				EvaluateContent: EvaluateContent.value,
				Type: 2
			}
		}).then(() => {
			hintData.value.ToStudentEvaluateScore = StudentEvaluateScore.value;
			hintData.value.ToStudentEvaluateContent = EvaluateContent.value;
			visibleEvaluate.value = false;
		})
	}
	
	function froom(item: any){
		console.log(item);
		classStore.setRoomConfirmDia(true);
		classStore.setActiveItem(item);
	}
	
	function uploadvideo(item: any){
		console.log(item);
		uni.chooseVideo({
			sourceType: ['camera', 'album'],
			maxDuration: 60,
			success: (chooseMediaRes) => {
				const tempFilePath = chooseMediaRes.tempFilePath;
				let uploadurl = '/ApiFilter/UploadFiles.ashx';
				// #ifdef MP-WEIXIN
				uploadurl = 'https://nx5.abc5g.com/ApiFilter/UploadFiles.ashx'
				// #endif
				uni.showLoading({
					title: '正在上传中',
					mask: true
				})
				uni.uploadFile({
					url: uploadurl, //仅为示例，非真实的接口地址
					filePath: tempFilePath,
					name: 'file',
					formData: { 
					},
					success: (uploadFileRes) => {
						var file = JSON.parse(uploadFileRes.data)[0].file; 
						ktlapi({ 
							action: "Cus_LesUpVideo",
							data: {
								"ID": item.UUID,
								"Video": 'https://nx5.abc5g.com' + file,
							}
						}).then(_res => {
							item.Video = 'https://nx5.abc5g.com' + file;
							uni.hideLoading();
						});
					},
					fail() {
						uni.hideLoading();
					}
				});
			}
		});
	}
</script>

<style lang="scss"> 
@import "../styles/class-list.scss";
</style>